Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: Fold "shift-by-zero" in lower #61222

Merged
merged 4 commits into from
Nov 5, 2021
Merged

JIT: Fold "shift-by-zero" in lower #61222

merged 4 commits into from
Nov 5, 2021

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Nov 4, 2021

Fixes a Checked-only assert found in #61209 by Fuzzlyn.

cc @jakobbotsch

A few diffs:

Top file improvements (bytes):
          -4 : 87027.dasm (-1.23% of base)
          -4 : 87338.dasm (-3.70% of base)
          -4 : 88327.dasm (-6.25% of base)
          -4 : 88191.dasm (-5.26% of base)

4 total files with Code Size differences (4 improved, 0 regressed), 0 unchanged.

Top method improvements (bytes):
          -4 (-6.25% of base) : 88327.dasm - DevDiv_534476.ILGEN_CLASS:ILGEN_METHOD(long):float
          -4 (-3.70% of base) : 87338.dasm - GitHub_18291:ILGEN_METHOD(long,byte,long):ushort
          -4 (-5.26% of base) : 88191.dasm - ILGEN_0x1f290143:Method_0x8252f06e(double,ushort,int,ubyte,float,long,int,long):int
          -4 (-1.23% of base) : 87027.dasm - ILGEN_CLASS:ILGEN_METHOD(ubyte,long,int):long

Top method improvements (percentages):
          -4 (-6.25% of base) : 88327.dasm - DevDiv_534476.ILGEN_CLASS:ILGEN_METHOD(long):float
          -4 (-5.26% of base) : 88191.dasm - ILGEN_0x1f290143:Method_0x8252f06e(double,ushort,int,ubyte,float,long,int,long):int
          -4 (-3.70% of base) : 87338.dasm - GitHub_18291:ILGEN_METHOD(long,byte,long):ushort
          -4 (-1.23% of base) : 87027.dasm - ILGEN_CLASS:ILGEN_METHOD(ubyte,long,int):long

e.g. https://www.diffchecker.com/oEoe5xQk

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 4, 2021
@ghost
Copy link

ghost commented Nov 4, 2021

Tagging subscribers to this area: @JulieLeeMSFT
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes a Checked-only assert found in #61209 by Fuzzlyn.

cc @jakobbotsch

Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@@ -5773,7 +5783,7 @@ void Lowering::LowerShift(GenTreeOp* shift)
assert(!cast->CastOp()->isContained());

// It has to be an upcast and CNS must be in [1..srcBits) range
if ((srcBits < dstBits) && ((UINT32)cns->IconValue() < srcBits))
if ((srcBits < dstBits) && (cns->IconValue() > 0) && (cns->IconValue() < srcBits))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly this is the fix and the removal of nop shifts is a separate optimization, or is that required as well?

FWIW, I would prefer we not do these ad-hoc opts in lowering (instead tracing back the where the frontend "failed at its job" and so on).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SingleAccretion it's pretty normal to duplicate peepholes accross phases and we already do it. Front-end didn't fail here, only rationalizer managed to fold this:

N036 ( 47, 59) [000024] -ACXG-------              \--*  LSH       int    <l:$2c8, c:$2d2>
N035 (  2,  5) [000067] -A--G-------                 \--*  COMMA     int   
N033 (  1,  3) [000042] -A--G---R---                    +--*  ASG       ref    <l:$1c2, c:$1d5>
N032 (  1,  1) [000041] D------N----                    |  +--*  LCL_VAR   ref    V02 tmp2         d:1 <l:$280, c:$85>
N031 (  1,  1) [000060] ------------                    |  \--*  LCL_VAR   ref    V03 cse0         u:1 <l:$280, c:$81>
N034 (  1,  2) [000066] ------------                    \--*  CNS_INT   int    0 <l:$2d1, c:$2d0>

into

N033 (  1,  3) [000042] DA--G-------              *  STORE_LCL_VAR ref    V02 tmp2         d:1
N034 (  1,  2) [000066] ------------        t66 =    CNS_INT   int    0 <l:$2d1, c:$2d0>
                                                  /--*  t9     int    
                                                  +--*  t66    int    
N036 ( 47, 59) [000024] ---XG-------        t24 = *  LSH       int    <l:$2c8, c:$2d2>
                                                  /--*  t24    int    
N037 ( 48, 60) [000025] ---XG-------              *  RETURN    int    $107

Copy link
Member Author

@EgorBo EgorBo Nov 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe some day we'll get a DSL for transformations which will be compiled into morph and lower at the same time (that's how some C++ compilers work today)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really worth to add this optimization in lower that only has hits in fuzzer generated code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakobbotsch not really, but the diff found 4 more cases and we still don't have collections for real-world apps to judge. but let me remove it since it raised concerns by you two

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants